package com.handler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.urule.console.DefaultPrincipal;
import com.bstek.urule.console.EnvironmentProvider;
import com.bstek.urule.console.Principal;
import com.bstek.urule.console.servlet.RequestContext;
import com.dao.RfileDao;

@Component
public class UruleSampleEnvironmentProvider implements EnvironmentProvider {

	@Autowired
	private RfileDao rfileDao;

	public Principal getLoginPrincipal(RequestContext requestContext) {
		DefaultPrincipal principal = new DefaultPrincipal();
		principal.setCompanyId(ContextHolder.getLoginUser().getCompanyId());
		principal.setName(ContextHolder.getLoginUserName());
		principal.setDisplayName(ContextHolder.getLoginUser().getCname());
		principal.setAdmin(ContextHolder.getLoginUser().isAdministrator());
		return principal;
	}

	public List<Principal> getPrincipals() {
		// TODO Auto-generated method stub
		List<Principal> principal = new ArrayList<Principal>();
		List<Map<String, Object>> iuser = rfileDao.loadAlluser();
		for (Map<String, Object> iu : iuser) {
			DefaultPrincipal dPrincipal = new DefaultPrincipal();
			dPrincipal.setCompanyId((String) iu.get("COMPANY_ID_"));
			dPrincipal.setName(((String) iu.get("USERNAME_")));
			dPrincipal.setDisplayName(((String) iu.get("CNAME_")));
			dPrincipal.setAdmin(ContextHolder.getLoginUser().isAdministrator());
			principal.add(dPrincipal);
		}
		return principal;
	}

}
